package com.zhj.rabbitmq.demo.controller;

import com.rabbitmq.client.AMQP;
import com.zhj.rabbitmq.demo.confirm.ConfirmRabbitmqConfig;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhengjibin
 * @version 1.0.0
 * @ClassName RabbitmqConfirmController.java
 * @Description rabbitmq 确认消息模式测试
 * @createTime 2023年03月25日 11:08:00
 */

@RestController
public class RabbitmqConfirmController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/confirm/send1")
    public String send1(String message){
        CorrelationData correlationData = new CorrelationData(System.currentTimeMillis()+"");
        for (int i=0;i<10;i++){
            int finalI = i;
            rabbitTemplate.convertAndSend(ConfirmRabbitmqConfig.EXCHANGE_NAME,ConfirmRabbitmqConfig.ROUTONG_KEY,
                    "第一次发送："+message+":序号("+i+")",msg ->{
                        msg.getMessageProperties().setPriority(finalI);//指定优先级
                        return msg;
                    },correlationData);
        }

        CorrelationData correlationData2 = new CorrelationData(System.currentTimeMillis()+"");
        //填写错误routingkey，模拟交换机 发送到队列失败
        rabbitTemplate.convertAndSend(ConfirmRabbitmqConfig.EXCHANGE_NAME,ConfirmRabbitmqConfig.ROUTONG_KEY+2,"第二次发送："+message,correlationData2);
        return "成功"+System.currentTimeMillis();
    }
}
